#include "RaiderManager.h"
#include <iostream>


RaiderManager::RaiderManager() {
}


RaiderManager::~RaiderManager() {
}



#pragma region publics
void RaiderManager::initialize(sf::RenderWindow* win, Player* play, RaiderBulletHandler* raid) {
	/*
	m_renderWindow = win;
	m_arrayOfRaiders = new raider[RAIDER_COUNT];
	for(int i = 0; i < RAIDER_COUNT; i++) {
		m_arrayOfRaiders[i].Initialize(win, play, raid);
	}

	m_clock = new sf::Clock();
	m_lastSpawn = 0.0f;
	*/
};

void RaiderManager::update() {
	spawnRaider();

	for(int i = 0; i < RAIDER_COUNT; i++) {
		if(m_arrayOfRaiders[i].IsBusy()) {
			m_arrayOfRaiders[i].Update();
		}
	}

};


void RaiderManager::cleanup() {
	delete m_clock;
	//delete m_random;

	for (int i = 0; i < RAIDER_COUNT; i++) {
		m_arrayOfRaiders[i].Cleanup();
	}
	delete[] m_arrayOfRaiders;

};

#pragma endregion



#pragma region privates


void RaiderManager::spawnRaider() {
	if(m_clock->getElapsedTime().asSeconds() - m_lastSpawn < 1) {
		return;
	}

	m_lastSpawn = m_clock->getElapsedTime().asSeconds();

	for(int i = 0; i < RAIDER_COUNT; i++) {
		if(!m_arrayOfRaiders[i].IsBusy()) {
			// Randomize spawn coordinates for enemies
			randomSide = (int)(rand() % 2);
			if (randomSide == 1) {
				randomX = (int)(rand() % 2560);
				randomCheckY = (int)(rand() % 2);
				if(randomCheckY == 1) {
					randomY = 0;
				} else if(randomCheckY == 0) {
					randomY = 1440;
				}
			} else if(randomSide == 0) {
				randomY = (int)(rand() % 1440);
				randomCheckX = (int)(rand() % 2);
				if(randomCheckX == 1) {
					randomX = 0;
				} else if(randomCheckX == 0) {
					randomX = 2560;
				}
			}
			//std::cout << randomX << "  " << randomY << std::endl;
			m_arrayOfRaiders[i].Spawn(randomX, randomY);
			break;
		}
	}
}


#pragma endregion
